package com.joysuch.wwyt.licence.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.licence.bean.LicenceModuleAuthDto;
import com.joysuch.wwyt.licence.entity.LicenceModuleAuth;
import com.joysuch.wwyt.licence.mapper.LicenceModuleAuthMapper;
import com.joysuch.wwyt.licence.service.LicenceModuleAuthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.util.AESUtil;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 模块授权表 服务实现类
 * </p>
 *
 * @author jialei
 * @since 2022-04-26
 */
@Service
public class LicenceModuleAuthServiceImpl extends ServiceImpl<LicenceModuleAuthMapper, LicenceModuleAuth> implements LicenceModuleAuthService {

    @Override
    public List<LicenceModuleAuthDto> listByTenentIdAngOrgCode(@NonNull Long tenentId, @NonNull String orgCode) {
        LambdaQueryWrapper<LicenceModuleAuth> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LicenceModuleAuth::getTenentId, tenentId);
        lambdaQueryWrapper.eq(LicenceModuleAuth::getOrgCode, orgCode);
        lambdaQueryWrapper.eq(LicenceModuleAuth::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());
        List<LicenceModuleAuth> list = this.list(lambdaQueryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return CollectionUtil.empty(null);
        }
        List<LicenceModuleAuthDto> resultList = new ArrayList<>(list.size());
        for (LicenceModuleAuth licenceModuleAuth : list) {
            LicenceModuleAuthDto authDto = new LicenceModuleAuthDto();
            authDto.setId(licenceModuleAuth.getId());
            authDto.setModuleCode(AESUtil.decrypt(licenceModuleAuth.getModuleCode(), BaseConfigCodes.LICENCE_AES_KEY));
            authDto.setModuleName(licenceModuleAuth.getModuleName());
            String startTimeDecryptStr = AESUtil.decrypt(licenceModuleAuth.getStartTime(), StrUtil.sub(StrUtil.fillAfter(licenceModuleAuth.getModuleCode(), 'w', 16), 0, 16));
            String endTimeDecryptStr = AESUtil.decrypt(licenceModuleAuth.getEndTime(), StrUtil.sub(StrUtil.fillAfter(licenceModuleAuth.getModuleCode(), 'w', 16), 0, 16));
            authDto.setStartTime(DateTime.of(Long.parseLong(startTimeDecryptStr)));
            authDto.setEndTime(DateTime.of(Long.parseLong(endTimeDecryptStr)));

            resultList.add(authDto);
        }

        return resultList;
    }

    @Override
    public List<LicenceModuleAuth> listByTenentIdAngOrgCodeAndModuleCodes(Long tenentId, String orgCode, Set<String> moduleCodeSet) {
        LambdaQueryWrapper<LicenceModuleAuth> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LicenceModuleAuth::getTenentId, tenentId);
        lambdaQueryWrapper.eq(LicenceModuleAuth::getOrgCode, orgCode);
        lambdaQueryWrapper.in(LicenceModuleAuth::getModuleCode, moduleCodeSet);
        lambdaQueryWrapper.eq(LicenceModuleAuth::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());

        return this.list(lambdaQueryWrapper);
    }
}
